//
//  CSYTextSettingView.swift
//  CSYEmoticonKeyboard__Test
//
//  Created by 姚彦兆 on 15/11/22.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYTextSettingView: UITextView {

    func emoticonToString() -> String{
    
        let attribute = attributedText
        
        var targetString : String = String()
        
        attribute.enumerateAttributesInRange(NSRange(location: 0, length: attributedText.length), options: []) { (dict, range, isEnd) -> Void in
            
//            print(dict)
            
//            print(range)
            
            if let attachment : CSYTextAttachment = dict["NSAttachment"] as? CSYTextAttachment{
            
                targetString += (attachment.chs ?? "")
            
            }else{
            
                targetString += (attribute.string as NSString).substringWithRange(range)
            
            }
            
        }
        
        return targetString
    
    }
    
    
    func insertTextAttachment( emoticonModel : CSYEmoticonModel ){
        
        if emoticonModel.isEmpty{
            
            return
            
        }
        
        if emoticonModel.isRemove{
            
            deleteBackward()
            
            return
            
        }
        
        if emoticonModel.defaultEmoticon != nil {
            
            replaceRange(selectedTextRange!, withText: emoticonModel.defaultEmoticon ?? "")
            
            return
            
        }
        
        //下面就是点击了图片表情的操作
        //处理图文混排
        //设置文本附件
//        let attachement : NSTextAttachment = NSTextAttachment()
//        //给文本附件添加图片
//        attachement.image = UIImage(contentsOfFile: emoticonModel.pngPath!)
//        
//        //设置控件Frame
//        let emoticon_H : CGFloat = font!.lineHeight
//        
//        let bounds : CGRect = CGRectMake(0, -4, emoticon_H, emoticon_H)
//        
//        attachement.bounds = bounds
//        
//        //将该附件设置为属性字符串
//        let attribute : NSMutableAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachement))
//        
//        attribute.addAttribute(NSFontAttributeName, value:font!, range: NSRange(location: 0,length: 1))
        
        
        let attachement : CSYTextAttachment = CSYTextAttachment()
        
        let attribute : NSAttributedString = attachement.getEmoticonAttachmentText(emoticonModel, font: font!)
        
        //获取输入框的属性文本
        let targetStr : NSMutableAttributedString = NSMutableAttributedString(attributedString: attributedText)
        
        let location : NSInteger = selectedRange.location
        
        targetStr.replaceCharactersInRange(selectedRange, withAttributedString: attribute)
        
        //        targetStr.replaceCharactersInRange(textInputView.selectedRange, withAttributedString: NSAttributedString(string: emoticonModel.chs!))
        
        attributedText = targetStr
        
        //        textInputView.selectedRange = NSRange(location: location + ((emoticonModel.chs)! as NSString).length, length: 0)
        
        selectedRange = NSRange(location: location + 1, length: 0)
        
    }


}
